﻿using System.Linq;
using JBeamLibrary.Tools;
using JBeamLibrary.VehicleParts.Collections;
using JBeamLibrary.VehicleParts.Elements;
using System.Collections.Generic;
using JBeamLibrary.VehicleParts.Interfaces;

namespace JBeamLibrary.VehicleParts.Groups
{
    public class Props : IVehicleGroup<Prop>
    {
        #region Fields

        private readonly Dictionary<int, Light> _lightList = new Dictionary<int, Light>();
        private readonly PropCollection _props = new PropCollection();

        #endregion

        #region Indexers

        public Prop this[int index]
        {
            get
            {
                if (Collection.Count == 0)
                    return null;
                if (index < 0 || index >= Collection.Count)
                    return null;
                return Collection[index];
            }
        }

        #endregion

        #region Properties

        public Dictionary<int, Light> LightList { get { return _lightList; } }

        public PropCollection Collection { get { return _props; } }

        public IVehicleGroupCollection<Prop> GroupCollection
        {
            get { return Collection; }
        }

        public int Count { get { return Collection.Count; } }

        public bool IsEmpty
        {
            get { return GroupHelper.GetPropertyLists(this).All(list => list.Count == 0) && Collection.Count == 0; }
        }

        #endregion

        #region Methods

        public void SetElementValue(int index, string propName, object value)
        {
            var elem = Collection[index];
            var prop = elem.GetType().GetProperty(propName);
            prop.SetValue(elem, value);
        }

        public object GetElementValue(int index, string propName)
        {
            var elem = Collection[index];
            var prop = elem.GetType().GetProperty(propName);
            return prop.GetValue(elem);
        }

        public IEnumerable<KeyValuePair<string, object>> GetElementValues(int index)
        {
            var elem = Collection[index];
            var props = elem.GetType().GetProperties();
            return props.Select(prop => new KeyValuePair<string, object>(prop.Name, prop.GetValue(elem))).AsEnumerable();
        }

        #endregion
    }
}
